package com.parktech.sun.web.controller;

import com.parktech.sun.domain.model.Constants;
import com.parktech.sun.domain.repository.Page;
import com.parktech.sun.domain.service.news.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * User: Think
 * Date: 11-1-15
 * Time: 下午7:55
 */
@Controller
@RequestMapping("/news")
public class NewsViewController  extends BaseViewController {

    private int pageSize = Constants.PAGINATION_PAGE_SIZE;

    private NewsService newsService;

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    @Autowired
    public NewsViewController(NewsService newsService) {
        this.newsService = newsService;
    }

    @RequestMapping("/{categoryId}/pages/{pageNo}")
    public String view(@PathVariable("categoryId") Integer categoryId, @PathVariable("pageNo") int pageNo, Model model) {
        Page page = new Page(pageSize, pageNo);
        model.addAttribute("categoryId", categoryId);
        model.addAttribute(newsService.pageBy(categoryId, page));
        return "news";
    }

    @RequestMapping("/{newsId}")
    public String viewDetail(@PathVariable("newsId") Long newsId, Model model) {
        model.addAttribute(newsService.getById(newsId));
        return "newsDetail";
    }
}
